package pers.zdb.ag.util;

import pers.zdb.ag.pojo.Point;

import java.util.List;

/**
 * @Description:
 * @author: Adobe Chow
 * @date: 2018/8/8 19:09
 * @Copyright: www.winshang.com Inc. All rights reserved.
 */
public class GaodeUtils {


    public static boolean IsPtInPoly(double ALon, double ALat, List<Point> APoints)
    {
        int iSum = 0, iCount;
        double dLon1, dLon2, dLat1, dLat2, dLon;
        if (APoints.size() < 3)
            return false;
        iCount = APoints.size();
        for (int i = 0; i < iCount - 1; i++)
        {
            if (i == iCount - 1)
            {
                dLon1 = APoints.get(i).getLongitude();
                dLat1 = APoints.get(i).getLatitude();
                dLon2 = APoints.get(0).getLongitude();
                dLat2 = APoints.get(0).getLatitude();
            }
            else
            {
                dLon1 = APoints.get(i).getLongitude();
                dLat1 = APoints.get(i).getLatitude();
                dLon2 = APoints.get(i+1).getLongitude();
                dLat2 = APoints.get(i+1).getLatitude();
            }
            //以下语句判断A点是否在边的两端点的水平平行线之间，在则可能有交点，开始判断交点是否在左射线上
            if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1)))
            {
                if (Math.abs(dLat1 - dLat2) > 0)
                {
                    //得到 A点向左射线与边的交点的x坐标：
                    dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat)) / (dLat1 - dLat2);

                    // 如果交点在A点左侧（说明是做射线与边的交点），则射线与边的全部交点数加一：
                    if (dLon < ALon)
                        iSum++;
                }
            }
        }
        if (iSum % 2 != 0)
            return true;
        return false;
    }

    public static class Poi{
        private Double lat;
        private Double lng;
        public Poi(Double lng,Double lat){
            this.lat = lat;
            this.lng = lng;
        }

    }


}
